package drone.iqgc.service ;

import java.util.ArrayList ;
import java.util.List ;

import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;

import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONObject ;

import drone.basis.util.StringUtil ;
import drone.iqgc.common.DroneManager ;
import io.mavsdk.mission.Mission ;
import io.mavsdk.mission.Mission.MissionItem ;
import io.mavsdk.mission.Mission.MissionItem.CameraAction ;

@Service
public class CommandService
{

	@Autowired
	private DroneManager droneManager ;
	
	
	// 状态指令
	public void droneStatusCommand (
			String id
	)
	{

		droneManager.executeStatus ( id ) ;

	}
	
	// 位置指令
	public void dronePositionCommand (
			String id
	)
	{

		droneManager.executePosition ( id ) ;

	}
	
	// 飞行模式
	public void droneFlightmodeCommand (
			String id
	)
	{

		droneManager.executeFlightmode ( id ) ;

	}
	
	// 最高速
	public void droneMaxspeedCommand (
			String id
	)
	{

		droneManager.executeMaxspeed ( id ) ;

	}
	
	// 起始点
	public void droneHomeCommand (
			String id
	)
	{

		droneManager.executeHome ( id ) ;

	}
	
	// 任务下载
	public void droneMissionDownloadCommand (
			String id
	)
	{
		
		droneManager.executeMissionDownload ( id ) ;

	}
	

	// 驶向指令
	public void droneGotoCommand (
			String id ,
			String speed ,
			String lng ,
			String lat
	)
	{

		droneManager.executeGoto ( id , Float.parseFloat ( speed ) , Double.parseDouble ( lng ) , Double.parseDouble ( lat ) ) ;

	}

	// 起飞指令
	public void droneTakeoffCommand (
			String id ,
			String alt
	)
	{

		droneManager.executeTakeoff ( id , Float.parseFloat ( alt ) ) ;

	}

	// 降落指令
	public void droneLandCommand (
			String id
	)
	{

		droneManager.executeLand ( id ) ;

	}

	// 返航指令
	public void droneReturnCommand (
			String id
	)
	{

		droneManager.executeReturn ( id ) ;

	}

	// 定高
	public void droneAltitudeCommand (
			String id ,
			String alt
	)
	{

		droneManager.executeAltitude ( id , Float.parseFloat ( alt ) ) ;

	}
	
	// 拍照
	public void droneCameraCommand (
			String id 
	)
	{

		droneManager.executeCamera ( id ) ;

	}

	// 任务上传
	public void droneMissionUploadCommand (
			String id ,
			JSONArray array
	) 
	{

		List < Mission.MissionItem > items = new ArrayList <> ( ) ;

		for ( int i = 0 ; i < array.size ( ) ; i ++ )
		{
			JSONObject task = array.getJSONObject ( i ) ;

			Double latitudeDeg = task.getDouble ( "lat" ) ;
			Double longitudeDeg = task.getDouble ( "lng" ) ;
			Float relativeAltitudeM = ( StringUtil.isEmpty ( task.get ( "relalt" ) ) || task.getFloat ( "relalt" ).isNaN ( ) ) ? Float.NaN : task.getFloat ( "relalt" ) ;
			Float speedMS = ( StringUtil.isEmpty ( task.get ( "speed" ) ) || task.getFloat ( "speed" ).isNaN ( ) ) ? Float.NaN : task.getFloat ( "speed" ) ;
			Boolean isFlyThrough = ( StringUtil.isEmpty ( task.get ( "through" ) ) || task.getString ( "through" ).equals ( "0" ) ) ? false : true ;
			Float gimbalPitchDeg = ( StringUtil.isEmpty ( task.get ( "gimpatch" ) ) || task.getFloat ( "gimpatch" ).isNaN ( ) ) ? Float.NaN : task.getFloat ( "gimpatch" ) ;
			Float gimbalYawDeg = ( StringUtil.isEmpty ( task.get ( "gimyaw" ) ) || task.getFloat ( "gimyaw" ).isNaN ( ) ) ? Float.NaN : task.getFloat ( "gimyaw" ) ;
			CameraAction cameraAction = CameraAction.valueOf ( task.getString ( "camera" ) ) ;
			Float loiterTimeS = ( StringUtil.isEmpty ( task.get ( "loiter" ) ) || task.getFloat ( "loiter" ).isNaN ( ) ) ? Float.NaN : task.getFloat ( "loiter" ) ;
			Double cameraPhotoIntervalS = ( StringUtil.isEmpty ( task.get ( "interval" ) ) || task.getDouble ( "interval" ).isNaN ( ) ) ? Double.NaN : task.getDouble ( "interval" ) ;
			Float acceptanceRadiusM = ( StringUtil.isEmpty ( task.get ( "radius" ) ) || task.getFloat ( "radius" ).isNaN ( ) ) ? Float.NaN : task.getFloat ( "radius" ) ;
			Float yawDeg = ( StringUtil.isEmpty ( task.get ( "yaw" ) ) || task.getFloat ( "yaw" ).isNaN ( ) ) ? Float.NaN : task.getFloat ( "yaw" ) ;
			Float cameraPhotoDistanceM = ( StringUtil.isEmpty ( task.get ( "distance" ) ) || task.getFloat ( "distance" ).isNaN ( ) ) ? Float.NaN : task.getFloat ( "distance" ) ;

			MissionItem item = new Mission.MissionItem ( latitudeDeg , longitudeDeg , relativeAltitudeM , speedMS , isFlyThrough , gimbalPitchDeg , gimbalYawDeg , cameraAction , loiterTimeS , cameraPhotoIntervalS , acceptanceRadiusM ,
					yawDeg , cameraPhotoDistanceM ) ;
			items.add ( item ) ;
		}

		Mission.MissionPlan missionPlan = new Mission.MissionPlan ( items ) ;

		droneManager.executeMissionUpload ( id , true , missionPlan ) ;

	}
	
	// 任务启动
	public void droneMissionStartCommand (
			String id
	)
	{

		droneManager.executeMissionStart ( id ) ;

	}
	
	// 任务撤销
	public void droneMissionCanelCommand (
			String id
	)
	{

		droneManager.executeMissionCanel ( id ) ;

	}

}
